import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
class VideoPlayerScreen extends StatefulWidget {
  final Object? arguments;

  const VideoPlayerScreen({super.key, this.arguments}); // 通过构造器接收参数
  @override
  _VideoPlayerScreenState createState() => _VideoPlayerScreenState();
}

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
  late VideoPlayerController _controller; // 视频播放控制器
  late Future<void> _initializeVideoPlayerFuture; // 初始化 Future

  @override
  void initState() {
    super.initState();
    final Map<String, dynamic>? args = widget.arguments as Map<String, dynamic>?;

    final String? url = args?['url'] as String?;

    if(url == null){
      return;
    }

    _controller = VideoPlayerController.networkUrl(
      Uri.parse(url), // 示例视频
    );
    // 初始化视频
    _initializeVideoPlayerFuture = _controller.initialize();
    // 设置循环播放（可选）
    _controller.setLooping(true);
  }

  @override
  void dispose() {
    // 释放资源
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('视频播放')),
      body: Center(
        child: FutureBuilder(
          future: _initializeVideoPlayerFuture,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              // 视频初始化完成
              return Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  AspectRatio(
                    aspectRatio: _controller.value.aspectRatio, // 保持视频宽高比
                    child: VideoPlayer(_controller), // 显示视频
                  ),
                  SizedBox(height: 20),
                  ElevatedButton(
                    onPressed: () {
                      setState(() {
                        if (_controller.value.isPlaying) {
                          _controller.pause(); // 暂停
                        } else {
                          _controller.play(); // 播放
                        }
                      });
                    },
                    child: Icon(
                      _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
                    ),
                  ),
                ],
              );
            } else {
              // 视频加载中
              return CircularProgressIndicator();
            }
          },
        ),
      ),
    );
  }
}